function login(url, gotoUrl) {
	var form = $("#login");
	var phone = $("input[name='phone']").val();
	var password = $("input[name='password']").val();
	if (phone != null && phone.length > 0 && password != null) {
		$.ajax({
			url : url,
			type : "post",
			dataType : "json",
			data : form.serialize(),
			success : function(json) {
				if (json.code == "success") {
					window.location.href = gotoUrl;
				} else {
					alert("账号密码错误！");
				}
			}
		});
	} else {
		alert("您有重要的信息没有填写哦！");
	}
}

function register(url) {
	var form = $("#register");
	var phone = $("input[name='utel']").val();
	var password = $("input[name='upassword']").val();
	if (phone != null && phone.length > 0 && password != null) {
		$.ajax({
			url : url,
			type : "post",
			dataType : "json",
			data : form.serialize(),
			success : function(json) {
				if (json.code == "success") {
					window.location.href = "../index";
				} else {
					alert("注册失败！");
				}
			}
		});
	}
}

/**
 * ajax请求菜单主页
 * 
 * @param url
 *            请求页的url
 */
function loadPage(url) {
	var main = $("#main");
	$.ajax({
		url : url,
		type : "get",
		dataType : "text",
		success : function(data) {
			main.empty();
			main.html(data);
		}
	});
}

/**
 * 加载products列表
 * 
 * @param url
 *            请求地址
 * @param page
 *            页数
 */
function loadProducts(url, page) {
	$.ajax({
		url : url,
		type : "get",
		dataType : "json",
		data : {
			page : page
		},
		success : function(json) {
			if (json.code == "success") {
				showProducts(json.products, json.total, page)
			} else {
				alert("请求失败！");
			}
		}
	});
}

/**
 * 渲染product列表
 * 
 * @param list
 *            product的list集合json数据
 * @param total
 *            总数
 * @param page
 *            页码
 */
function showProducts(list, total, page) {
	this.total = total;
	this.page = page;
	initPage(page, total, number);
	// 散标table表初始化
	var products = $("#products");
	$.each(list, function(index, value) {
		var tbody = products.find("tbody");
		tbody.append("<tr onclick='gotoDetailPage(" + value.productid
				+ ")'></tr>");
		var tr = tbody.find("tr:last");
		tr.append("<td>" + value.pname + "</td>");
		tr.append("<td>" + value.prate + "%</td>");
		tr.append("<td>" + value.ppricetotal + "元</td>");
		tr.append("<td>" + value.ptimelimit + "个月</td>");
		var priceTotal = parseFloat(value.ppricetotal);
		var currentPrice = parseFloat(value.ppricecurrent);
		tr.append("<td>" + ((currentPrice / priceTotal).toFixed(3)) * 100
				+ "%</td>");
		var obj;
		if (value.ppricetotal > value.ppricecurrent) {
			obj = $("<span>投标</span>");
		} else {
			if (value.pisdone == true) {
				obj = $("<span>已满标</span>")
			} else {
				obj = $("<span>还款中</span>")
			}
		}
		tr.append("<td></td>");
		tr.find("td:last").append(obj);
	});
}

function initPage(page, total, number) {
	// 判断当前页是否是第一页
	var pre = $("#pre");
	if (page == 1) {
		pre.attr("class", "disabled");
	} else {
		pre.attr("class", "");
	}
	// 判断当前页是否是最后一页
	var maxPage = parseInt(total % number == 0 ? total / number : total
			/ number + 1);
	var next = $("#next");
	if (page == maxPage) {
		next.attr("class", "disabled");
	} else {
		next.attr("class", "");
	}
}

/**
 * 前往detail页面
 * 
 * @param id
 *            product的id
 */
function gotoDetailPage(id) {
	var url = "index?action=detail&id=" + id;
	window.open(url);
}

/**
 * 投标
 * 
 * @param url
 *            地址
 * @param money
 *            投标金额
 * @param productId
 *            product的id
 */
function buyProduct(url, money, productId) {
	$.ajax({
		url : url,
		type : "post",
		dataType : "json",
		data : {
			money : money,
			productId : productId
		},
		success : function(json) {
			if (json.code == "success") {
				alert("购买成功！");
				location.reload(true);
			} else {
				alert("购买失败！");
			}
		},
		error : function(json) {
			alert("购买失败,请登录！");
		}
	});
}

function getCash() {
	alert("提现成功！");
}

function loadRecords(url, page) {
	$.ajax({
		url : url,
		type : "get",
		dataType : "json",
		data : {
			page : page
		},
		success : function(json) {
			if (json.code == "success") {
				showRecords(json.records, page, json.total);
			} else {
				alert("请求失败！");
			}
		}
	});
}

function showRecords(list, page, total) {
	this.page = page;
	this.total = total;
	initPage(page, total, number);
	var records = $("#records");
	$.each(list, function(index, value) {
		var tbody = records.find("tbody");
		tbody.append("<tr></tr>");
		var tr = tbody.find("tr:last");
		tr.append("<td>"
				+ (new Date(value.ftimedeal)).format("yyyy-MM-dd hh:mm:ss")
				+ "</td>");
		if (value.productid > 0) {
			tr.append("<td>" + value.ftypedeal + "(" + value.productid + ")"
					+ "</td>");
		} else {
			tr.append("<td>" + value.ftypedeal + "</td>");
		}
		if (value.ftypedeal == "投资" || value.ftypedeal == "返现") {
			tr.append("<td> -" + value.fmoneydeal + "</td>");
		} else {
			tr.append("<td> +" + value.fmoneydeal + "</td>");
		}
		tr.append("<td>" + value.fmoneyleft + "</td>");
	});
}

function cashOpera(url, type, cash) {
	$.ajax({
		url : url,
		type : "post",
		dataType : "json",
		data : {
			cash : cash,
			type : type
		},
		success : function(json) {
			if (json.code == "success") {
				alert("操作成功！");
				location.reload(true);
			} else {
				alert("操作失败！");
			}
		}
	});
}

/**
 * 时间对象的格式化;
 */
Date.prototype.format = function(format) {
	/*
	 * eg:format="YYYY-MM-dd hh:mm:ss";
	 */
	var o = {
		"M+" : this.getMonth() + 1, // month
		"d+" : this.getDate(), // day
		"h+" : this.getHours(), // hour
		"m+" : this.getMinutes(), // minute
		"s+" : this.getSeconds(), // second
		"q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
		"S" : this.getMilliseconds()
	// millisecond
	}

	if (/(y+)/.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
	}

	for ( var k in o) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
					: ("00" + o[k]).substr(("" + o[k]).length));
		}
	}
	return format;
}

function queryUser(url) {
	var form = $("#query");
	$.ajax({
		url : url,
		type : "get",
		dataType : "json",
		data : form.serialize(),
		success : function(json) {
			if (json != null) {
				showUser(json);
			}
		}
	});
}

function showUser(json) {
	var root = $("#root").val();
	var tbody = $("#users").find("tbody");
	tbody.empty();
	tbody.append("<tr></tr>");
	var tr = tbody.find("tr:last");
	tr.append("<td>" + json.userid + "</td>");
	tr.append("<td>" + json.utel + "</td>");
	tr.append("<td>" + json.upassword + "</td>");
	tr.append("<td>" + json.umoney + "</td>");
	tr.append("<td>" + json.urealname + "</td>");
	tr.append("<td>" + json.uidentification + "</td>");
	tr.append("<td>" + json.uoccupation + "</td>");
	tr.append("<td>" + (new Date(json.uregtime)).format("yyyy-MM-dd hh:mm:ss")
			+ "</td>");
	tr.append("<td>" + "<a href='javascript:getUser(\"" + root
			+ "/back/user/queryById?id=" + json.userid + "\")'>修改</a> "
			+ "<a href='javascript:delete(\"" + root
			+ "/back/user/deleteUser?id=" + json.userid + "\")'>删除</a>"
			+ "</td>");
}

/**
 * 删除用户
 * 
 * @param url
 */
function deleteUser(url) {
	var sure = confirm("确定需要删除该用户？");
	if (sure == true) {
		var root = $("#root").val();
		$.get(url, function(json) {
			if (json.code == "success") {
				alert("删除成功！");
				loadPage(root + "/back/user/queryUser");
			}
		}, "json");
	}
}
function addUser(url) {
	var form = $("#userForm");
	var root = $("#root").val();
	$.ajax({
		url : url,
		type : "post",
		dataType : "json",
		data : form.serialize(),
		success : function(json) {
			if (json.code == "success") {
				alert("添加成功！");
				loadPage(root + "/back/user/queryUser");
			}
		},
		error : function() {
			alert("添加失败！");
		}
	});
}

function getUser(url) {
	$.get(url, function(json) {
		if (json != null) {
			$("input[name='utel']").val(json.utel);
			$("input[name='upassword']").val(json.upassword);
			$("input[name='umoney']").val(json.umoney);
			$("input[name='urealname']").val(json.urealname);
			$("input[name='uidentification']").val(json.uidentification);
			$("input[name='uoccupation']").val(json.uoccupation);
			$("input[name='userid']").val(json.userid);
			$("#changeMsg").modal("show");
		}
	}, "json");
}

/**
 * 修改用户
 * 
 * @param url
 */
function updateUser(url) {
	var form = $("#changeForm");
	var root = $("#root").val();
	$.post(url, form.serialize(), function(json) {
		if (json.code == "success") {
			alert("修改成功！");
			$("#changeMsg").on("hidden.bs.modal", function() {
				loadPage(root + "/back/user/queryUser");
			});
			$("#changeMsg").modal("hide");
		} else {
			alert("修改失败！");
		}
	}, "json");
}

function queryProduct(url) {
	var form = $("#query");
	$.ajax({
		url : url,
		type : "get",
		dataType : "json",
		data : form.serialize(),
		success : function(json) {
			if (json != null) {
				showProduct(json);
			}
		}
	});
}

function showProduct(json) {
	var root = $("#root").val();
	var products = $("#products");
	var tbody = products.find("tbody");
	tbody.empty();
	tbody.append("<tr></tr>");
	var tr = tbody.find("tr:last");
	tr.append("<td>" + json.productid + "</td>");
	tr.append("<td>" + json.pname + "</td>");
	tr.append("<td>" + json.ppricetotal + "</td>");
	tr.append("<td>" + json.prate + "%</td>");
	tr.append("<td>" + json.ptimelimit + "</td>");
	tr.append("<td>" + json.pdesc + "</td>");
	var type = "";
	if (json.ptype == 1) {
		type = "到期还本息";
	} else if (json.ptype == 2) {
		type = "每月还利息";
	} else {
		type = "每月还本利";
	}
	tr.append("<td>" + type + "</td>");
	tr.append("<td>" + json.ppricecurrent + "</td>");
	tr.append("<td>" + json.ppeoplecurrent + "</td>");
	var done = "";
	if (json.pisdone == false) {
		done = "否";
	} else {
		done = "是";
	}
	tr.append("<td>" + done + "</td>");
	tr.append("<td>" + "<a href='javascript:getProduct(\"" + root
			+ "/back/product/queryById?id=" + json.productid + "\")'>修改</a> "
			+ "<a href='javascript:deleteProduct(\"" + root
			+ "/back/product/delete?id=" + json.productid + "\")'>删除</a>"
			+ "</td>");
}

function addProduct(url) {
	var form = $("#productForm");
	var root = $("#root").val();

	$.post(url, form.serialize(), function(json) {
		if (json.code == "success") {
			alert("添加成功！");
			loadPage(root + "/back/product/queryProduct");
		} else {
			alert("添加失败！");
		}
	}, "json");
}

function deleteProduct(url) {
	var root = $("#root").val();
	var sure = confirm("确定需要删除该散标？");
	if (sure == true) {
		$.get(url, function(json) {
			if (json.code == "success") {
				alert("删除成功！");
				loadPage(root + "/back/product/queryProduct");
			}
		}, "json");
	}
}

function getProduct(url) {
	$.get(url, function(json) {
		$("input[name='pname']").val(json.pname);
		$("input[name='ppricetotal']").val(json.ppricetotal);
		$("input[name='prate']").val(json.prate);
		$("input[name='ptimelimit']").val(json.ptimelimit);
		$("input[name='pdesc']").val(json.pdesc);
		var options = $("select[name='ptype']").find("option");
		options.eq(json.ptype - 1).attr("selected", "selected");
		$("input[name='productid']").val(json.productid);
		$("#changeMsg").modal("show");
	}, "json");
}

function updateProduct(url) {
	var root = $("#root").val();
	$.post(url, $("#changeForm").serialize(), function(json) {
		if (json.code == "success") {
			alert("修改成功！");
			$("#changeMsg").on("hidden.bs.modal", function() {
				loadPage(root + "/back/product/queryProduct");
			});
			$("#changeMsg").modal("hide");
		} else {
			alert("修改失败！");
		}
	}, "json");
}

function queryInvest(url) {
	var form = $("#query");
	$.get(url, form.serialize(), function(json) {
		if (json != null) {
			showInvest(json);
		}
	}, "json");
}

function showInvest(json) {
	var table = $("#invests");
	var tbody = table.find("tbody");
	tbody.empty();
	tbody.append("<tr></tr>");
	var tr = tbody.find("tr:last");
	tr.append("<td>" + json.investid + "</td>");
	tr.append("<td>" + json.productName + "</td>");
	tr.append("<td>" + json.realName + "</td>");
	tr.append("<td>" + json.ilendmoney + "</td>");
	tr.append("<td>" + (new Date(json.iinvesttime)).format("yyyy-MM-dd hh:mm:ss") + "</td>");
}